package com.woniu.elderly.serve.consumer;

import com.rabbitmq.client.Channel;
import com.woniu.common.entity.DealOrder;
import com.woniu.common.entity.ServePerson;
import com.woniu.common.entity.ServeWorkOrder;
import com.woniu.elderly.serve.feignclient.DealOrderFeign;
import com.woniu.elderly.serve.feignclient.ElderlyServiceFeign;
import com.woniu.elderly.serve.service.ServePersonService;
import com.woniu.elderly.serve.service.ServeWorkOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;


@Component
public class ElderlyServiceConsumer {
    @Autowired
    private ServePersonService servePersonService;

    @Autowired
    private ServeWorkOrderService serveWorkOrderService;

    @Autowired
    private DealOrderFeign dealOrderFeign;

    @RabbitListener(queues = "queuesendorder")
    @GlobalTransactional(name = "fsp_tx_group",rollbackFor = Exception.class)
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void doQueuesEndOrderDlx(ServeWorkOrder serveWorkOrder, Channel channel, Message message) throws Exception {
        System.out.println("-----------------处理队列---------------------");
        System.out.println("serveWorkOrder:" + serveWorkOrder);
        Integer pid = servePersonService.random(serveWorkOrder.getServeproject());
        System.out.println(message.getMessageProperties().getDeliveryTag());
        if(pid > 0){
            //获取服务人员id进行消息确认并修改服务人员状态
            System.out.println("有服务人员");

            //进行工单添加
            serveWorkOrder.setPid(pid);
            serveWorkOrder.setState("服务中");
            int wid = serveWorkOrderService.add(serveWorkOrder);

            //修改服务人员状态
            ServePerson servePerson = new ServePerson();
            servePerson.setPid(pid);
            servePerson.setState("服务中");
            servePerson.setWid(wid);
            servePersonService.modify(servePerson);

            //将工单信息返回给订单
            //获取当前系统时间作为接单时间
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = dateFormat.format(date);

            DealOrder dealOrder = new DealOrder();
            dealOrder.setOrderId(serveWorkOrder.getOid());
            dealOrder.setServerId(pid);
            dealOrder.setOrderAcceptdate(time);
            dealOrder.setOrderStatus("已接单");

            //将改好的订单信息传给订单微服务
            dealOrderFeign.modify(dealOrder);

            //手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }else{
            //服务人员不足,重回队列
            System.out.println("没有服务人员");
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }
}
